﻿using Microsoft.VisualStudio.TestTools.UITest.Common;
using System;
using System.Collections.Generic;

namespace AXExtension.Filters
{
	/// <summary>
	/// Represents a much more high level abstraction
	/// of an action filter, with some nice bells and
	/// whistles.
	/// </summary>
	abstract class Filter
	{
		private List<Func<IUITestActionStack, bool>> requirements = new List<Func<IUITestActionStack, bool>>();

		public IEnumerable<Func<IUITestActionStack, bool>> Requirements { get { return requirements; } }

		public Filter()
		{
			Require(stack => stack.Count > 0);
			Require(stack => stack.Peek().UIElement != null);
		}

		protected void Require(Func<IUITestActionStack, bool> requirement)
		{
			requirements.Add(requirement);
		}

		/// <summary>
		/// UnconditionalRun is invoked before the 
		/// requirements are checked and before Run itself.
		/// </summary>
		public virtual void UnconditionalRun(IUITestActionStack stack) { }

		/// <summary>
		/// Run is invoked only if (and after) all of the
		/// requirements evaluate to true.
		/// </summary>
		public abstract void Run(IUITestActionStack stack);
	}
}
